package com.fenghongzhang.day002.db;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.fenghongzhang.day002.R;

import org.greenrobot.greendao.query.Query;

import java.util.List;
import java.util.function.Consumer;

public class Main3Activity extends AppCompatActivity {

    private static final String TAG = "Main3Activity";
    private UserDao userDao;
    private StudentDao studentDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "user.db");
        SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
//        devOpenHelper.onUpgrade(writableDatabase,1,2);
        DaoMaster daoMaster = new DaoMaster(writableDatabase);
        DaoSession daoSession = daoMaster.newSession();
        userDao = daoSession.getUserDao();

         studentDao = daoSession.getStudentDao();
    }

    public void add(View view) {

        Student student = new Student();
        student.setName("哈哈");
        student.setAddress("呵呵呵呵呵呵呵");
        studentDao.insert(student);


//        User user = new User();
////        user.setId(10l);
//        user.setName("小明88");
//        user.setPwd("1111");
//        userDao.insert(user);
//        userDao.insertOrReplace(user);
//        userDao.delete(user);
    }

    public void query(View view) {
        //查询所有
//        List<User> users = userDao.loadAll();
//        Toast.makeText(this, ""+users.size(), Toast.LENGTH_SHORT).show();

        //查询指定的数据
//        userDao.loadByRowId()
//        userDao.queryBuilder().where("name = ?")
//        User user = userDao.queryBuilder().where(UserDao.Properties.Name.eq("小明88")
//                ,UserDao.Properties.Pwd.eq("1111")).build().uniqueOrThrow();
//        Log.i(TAG, "query: "+user.toString());

        //查询多个
//        List<User> users = userDao.queryBuilder().where(UserDao.Properties.Name.eq("小明88")).build().list();
//
//        users.forEach(new Consumer<User>() {
//            @Override
//            public void accept(User user) {
//                Log.i(TAG, "accept: "+user.toString());
//            }
//        });

//        List<User> users = userDao.queryBuilder().where(UserDao.Properties.Name.eq("小明88")).offset(3*2).limit(2).build().list();


        //原生查询
//        List<User> users = userDao.queryRaw("where _id = ? and name = ?", "10","小明88");
//        User user = users.get(0);


        User user = userDao.queryBuilder().where(UserDao.Properties.Id.eq(10l)).build().unique();
        Log.i(TAG, "query: "+user.toString());


    }

    public void del(View view) {
        userDao.deleteByKey(10l);

//        User user = new User();
//        user.setId(10l);
//        user.setName("小明88");
//        user.setPwd("1111");
//        userDao.delete(user);
    }

    public void update(View view) {
        User oldUser = userDao.queryBuilder().where(UserDao.Properties.Id.eq(10l)).build().unique();
        User user = new User();
        user.setId(10l);
        user.setName("小明66");
        user.setPwd(oldUser.getPwd());
        userDao.update(user);
    }
}
